﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Fleck;

namespace FleckServer
{
	class Program
	{
		static void Main(string[] args)
		{
			FleckLog.Level = LogLevel.Debug;
			var allSockets = new List<IWebSocketConnection>();
			//var server = new WebSocketServer("ws://0.0.0.0:8888");
			var server = new WebSocketServer("ws://192.168.5.143:8888");
			server.Start(socket =>
			{
				socket.OnOpen = () =>
				{
					Console.WriteLine("Open!");
					allSockets.Add(socket);
				};
				socket.OnClose = () =>
				{
					Console.WriteLine("Close!");
					allSockets.Remove(socket);
				};
				socket.OnMessage = message =>
				{
					Console.WriteLine(message);
					allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
				};
			});

			Console.WriteLine("**************************");
			Console.WriteLine("Input exit to stop server!");
			Console.WriteLine("**************************");
			var input = Console.ReadLine();
			while (input != "exit")
			{
				foreach (var socket in allSockets.ToList())
				{
					socket.Send(input);
				}
				input = Console.ReadLine();
			}
		}
	}
}
